<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Http\Repositories\Business\BusinessClaim;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Models\Business\BusinessList;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class BusinessClaimController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BusinessClaim(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid')->display(function ($value) {
                $memberInfo = (new MemberList())->getDetails($value);
                $member = "认领名称：{$memberInfo->username}<br>";
                $member .= "认领手机号：{$this->mobile}<br>";
                $member .= "认领姓名：" . $this->realname;
                return $member;
            });

            $grid->column('verinfo');
            $grid->column('shid')->display(function ($value) {
                $business = (new BusinessList())->getDetails($value);
                $businessInfo = "商家名称：{$business->name}<br>";
                $businessInfo .= "联系电话：{$business->tel}<br>";
                $businessInfo .= "地区：".$business->province . "/";
                if ($business->city){
                    $businessInfo .= $business->city . "/";
                }
                if ($business->district){
                    $businessInfo .= $business->district. "/";
                }
                if ($business->street){
                    $businessInfo .= $business->street. "/";
                }
                if ($business->street_number){
                    $businessInfo .= $business->street_number. "/";
                }
                if ($business->addr){
                    $businessInfo .= $business->addr;
                }
                if ($business->lat && $business->lng){
                    $businessInfo .= "<br>经纬度：".$business->lat . "/". $business->lng;
                }

                return $businessInfo;

            });
            $grid->column('status')->switch();
            $grid->column('status_info')->display(function ($val){
                return $val ?? '请输入审核说明';
            })->editable(true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BusinessClaim(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('realname');
            $show->field('mobile');
            $show->field('verinfo');
            $show->field('status');
            $show->field('status_info');
            $show->field('shid');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessClaim(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('realname');
            $form->text('mobile');
            $form->text('verinfo');
            $form->text('status');
            $form->text('status_info');
            $form->text('shid');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
